package adaptive.opencom;

import OpenCOM.IConnections;
import OpenCOM.ILifeCycle;
import OpenCOM.IMetaInterface;
import OpenCOM.IUnknown;
import adaptive.ime.MyKeyboardService.KeyboardSize;
import adaptive.opencom.AdaptationRequest.AdaptationType;
import adaptive.opencom.log.KeyLog;
import android.util.Log;

public class ThreeErrorKeyboardPolicyComponent extends AbstractKeyboardPolicyComponent
	implements IAdaptationPolicy, IUnknown, ILifeCycle, IMetaInterface, IConnections {

	public ThreeErrorKeyboardPolicyComponent(IUnknown mpIOCM) {
		super(mpIOCM);
	}

	// ILifeCycle operations
	@Override
	public boolean startup(Object data) {
		return true;
	}

	@Override
	public boolean shutdown() {
		return true;
	}

	// IAdaptationPolicy Operations
	@Override
	public AdaptationRequest adaptationRequest() {
		Log.d("AdaptationRequest", "ThreeErrorKeyboardPolicy");
		int errorCount = 0;
	
		for(KeyLog keyLog : rcEventMonitor.m_pIntf.getKeyboardLog()) {
			if(keyLog.getKeyCode() == -5) {
				errorCount++;
				Log.d("ErrorCount", String.valueOf(errorCount));
				if(errorCount == 3)
					break;
			}
		}
		
		// Check for adaptation request
		if(errorCount == 3) {
			Log.d("Required", "ThreeError");
			rcEventMonitor.m_pIntf.getKeyboardLog().clear();
			
			// Choose a Larger keyboard
			KeyboardSize newSize;
			switch(mSize) {
				case SMALL:
					newSize = KeyboardSize.MEDIUM;
					break;
				case MEDIUM:
					newSize = KeyboardSize.LARGE;
					break;
				case LARGE:
					newSize = KeyboardSize.SMALL;
					break;
				default:
					newSize = KeyboardSize.LARGE;
			}
			mSize = newSize;
			return new AdaptationRequest(AdaptationType.KEYBOARD_SIZE, newSize);
		} else 
			return new AdaptationRequest();
	}
}
